package ArrayList.work01;

import java.util.ArrayList;
import java.util.Scanner;
/**
 * 1、学生类信息有 学号，年龄，姓名，身份证。其中学号是唯一的。
 * 实现在控制台的学员信息管理，包括学员信息添加(判断是否重复)，修改，删除*，和全部显示。
 * 使用ArrayList<E>保存学生对象信息
 * 效果类似：
 * 请选择您的操作
 * 1、显示全部学员
 * 2、删除
 * 3、增加
 * 4、修改
 * 0、退出
 */
public class Student {
    private int age;//年龄
    private String cardId;//身份证号
    private String stuNo;//学号
    private String name;//姓名


    public Student() {
    }

    public Student(int age, String cardId, String stuNo, String name) {
        this.age = age;
        this.cardId = cardId;
        this.stuNo = stuNo;
        this.name = name;
    }

    public Student(String stuId, int age, String name, String cardId) {

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    static ArrayList<Student> studentList = new ArrayList<>();
    //初始化学员信息
    static {
        studentList.add(new Student( 23,"0001","张三","340827199101010234"));
        studentList.add(new Student( 22,"0002","李四","340827199101010235"));
        studentList.add(new Student( 20,"0003","王五","340827199101010236"));
    }



    //显示全部学员
    public static void show() {
        System.out.println("学号\t\t年龄\t\t姓名\t\t\t身份证号码");
        for (int i = 0; i < studentList.size(); i++) {
            System.out.println(studentList.get(i).stuNo + "\t" + studentList.get(i).age
                    + "\t\t" + studentList.get(i).name + "\t\t" + studentList.get(i).cardId);
        }
    }

    //删除学员
    public static void delete(String stuNo) {
        for (int i = 0; i < studentList.size(); i++) {
            if (stuNo.equals(studentList.get(i).getStuNo())) {
                studentList.remove(i);
                return;
            }
        }
    }

    //增加学员(判断是否重复)
    public static void creat(Student student) {
        for (int i = 0; i < studentList.size(); i++) {
            if(student.getStuNo().equals(studentList.get(i).getStuNo())){
                throw new RuntimeException("学员信息重复，无法添加");
            }else{
                studentList.add(student);
                return;
            }
        }
    }

    //修改学员信息
    public static void update(String oldStuNo, String newStuNo) {
        int index = -1;
        for (int i = 0; i < studentList.size(); i++) {
            if (oldStuNo.equals(studentList.get(i).getStuNo())) {
                studentList.get(i).setStuNo(newStuNo);
                return;
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = "";
        do{
            System.out.println("1.显示全部学员\t2.删除\t3.增加\t4.修改\t0.退出");
            System.out.print("请选择：");
            int choose = input.nextInt();
            switch (choose){
                case 1:
                    show();
                    break;
                case 2:
                    System.out.print("请输入要删除学生的学号：");
                    String stuNo = input.next();
                    delete(stuNo);
                    break;
                case 3:
                    System.out.print("请输入要添加的学员学号：");
                    String stuId = input.next();
                    System.out.print("请输入要添加的学员年龄：");
                    int age = input.nextInt();
                    System.out.print("请输入要添加的学员姓名：");
                    String name = input.next();
                    System.out.print("请输入要添加的学员身份证号：");
                    String cardId = input.next();
                    Student student = new Student(stuId,age,name,cardId);
                    try {
                        creat(student);
                        System.out.println("添加学员成功！");
                    }catch (RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 4:
                    System.out.print("请输入修改前的学生学号：");
                    String oldStuNo = input.next();
                    System.out.print("请输入修改后的学生学号：");
                    String newStuNo = input.next();
                    update(oldStuNo,newStuNo);
                    break;
            }
            System.out.print("是否继续操作(Y/N):");
            str = input.next();
        }while("Y".equalsIgnoreCase(str));
    }
}
